package com.example.hbutliving.service.question;

import com.example.hbutliving.config.OfficialMessageType;
import com.example.hbutliving.config.qiniu.QiNiuConfig;
import com.example.hbutliving.domain.send.DingMessage;
import com.example.hbutliving.mapper.question.AnswerDao;
import com.example.hbutliving.mapper.question.QuestionDao;
import com.example.hbutliving.service.BaiDuContentCheckService;
import com.example.hbutliving.service.dingtalk.SendDingMessageService;
import lombok.extern.slf4j.Slf4j;
import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

@Slf4j
@Service
public class QuestionVideoCheckService extends BaiDuContentCheckService {
    @Autowired
    private QuestionDao questionDao;

    @Autowired
    private AnswerDao answerDao;


    @Async("checkContent")
    public void checkQuestionVideo(String[] filenames, int questionId, int publisherId) {
        for (String filename : filenames) {
            String url = QiNiuConfig.URL + filename;
            JSONObject jsonObject = client.videoCensorUserDefined(filename, url, url, null);
            String conclusion = (String) jsonObject.get("conclusion");
            log.info(jsonObject.toString(2));
            if ("不合规".equals(conclusion)) {
                //删除动态
                questionDao.delete(questionId);
                //给发布者发布官方消息
                officialMessageService.sendOfficialMessage("您发布的问题含有违规内容,已被管理员删除", null, publisherId, OfficialMessageType.PUNISH);
                SendDingMessageService.sendContentAuditMessage(new DingMessage(publisherId + "号用户发布的" + questionId + "号问题视频内容审核未通过"));
                return;
            }
        }

    }
    @Async("checkContent")
    public void checkAnswerVideo (String[]filenames,int answerId, int publisherId){
        for (String filename : filenames) {
            String url = QiNiuConfig.URL + filename;
            JSONObject jsonObject = client.videoCensorUserDefined(filename, url, url, null);
            String conclusion = (String) jsonObject.get("conclusion");
            log.info(jsonObject.toString(2));
            if ("不合规".equals(conclusion)) {
                //删除动态
                answerDao.delete(answerId);
                //给发布者发布官方消息
                officialMessageService.sendOfficialMessage("您发布的回答含有违规内容,已被管理员删除", null, publisherId, OfficialMessageType.PUNISH);
                SendDingMessageService.sendContentAuditMessage(new DingMessage(publisherId + "号用户发布的" + answerId + "号回答视频内容审核未通过"));
                return;
            }
        }
    }
}
